#include "StdAfx.h"
#include "PhotoDlg.h"
#include "WceUiButton.h"
#include "WEGlobal.h"
#include "WceUiWndAnimate.h"
#include "WceUiSliderAni.h"
#include "WceUiRealWnd.h"

#ifdef _WIN32_WCE
	#define FB_ROOT_DIR L"\\"
#else
	#define FB_ROOT_DIR L"C:\\"
#endif
const int g_nframe_left = 0;
const int g_nframe_top  = 67;

CPhotoDlg::CPhotoDlg(void)
{
}

CPhotoDlg::~CPhotoDlg(void)
{
	UnInitList();
}

void CPhotoDlg::OnInitDialog()
{
	m_pFrame = GetChildByName(L"item_frame");
	ASSERT(NULL != m_pFrame);
	
	int i = 0;
	for (i = 0; i < PHOTO_LIST_COUNT; i++)
	{
		TCHAR szName[32] = {0};
		_stprintf(szName, L"item%d", i + 1);
		m_pPhotoItemListArr[i] = m_pFrame->GetChildByName(szName);
		ASSERT(NULL != m_pPhotoItemListArr[i]);

		CRect rc(0, i * 41, 480, i * 41 + 41);
		m_pPhotoItemListArr[i]->GetCommonStyle()->SetPosition(&rc, WCEUI_RELATIVE_POSITION);
		m_pPhotoItemListArr[i]->GetCommonStyle()->SetUsePosition(TRUE);
		m_pPhotoItemListArr[i]->GetStyle()->SetPosition(&rc, WCEUI_RELATIVE_POSITION);
		m_pPhotoItemListArr[i]->SetText(szName);

		CWceUiBgItem bgItem;
		CRect rcBgItem(15, 0, 15 + 29, 41);
		bgItem.SetDstRect(&rcBgItem);
		bgItem.SetAlign(DT_VCENTER|DT_CENTER); 
		bgItem.SetCommonStyleIndex(100);
		m_pPhotoItemListArr[i]->GetCommonStyle()->GetBgItemCtrl()->AddHead(bgItem);
		m_pPhotoItemListArr[i]->GetCommonStyle()->SetUseBgItemCtrl(TRUE);

	}

	InitList(FB_ROOT_DIR);
	UpdateList();

}


void CPhotoDlg::OnClick(PCWceUiButton pButton)
{
	if (pButton->IsEqualName(L"back"))
	{
		EndDialog(0);
	}
}

LRESULT CPhotoDlg::OnCommand(UINT uCommand, WPARAM wParam, LPARAM lParam)
{
	if (CMD_WE_I_MOVED == uCommand)
	{
		UpdateList();
	}else if (CMD_WE_I_CLICK == uCommand)
	{
		CWceUiLayer* pDownLayer = (CWceUiLayer*)wParam;
		ASSERT(NULL != pDownLayer);
		CString strPath;

		CString strText = pDownLayer->GetText();

		if (!strText.IsEmpty())
		{
			BOOL bUpLevel = FALSE;
			if (0 == strText.Compare(L".."))
			{
				TCHAR szNewPath[MAX_PATH] = {0};
				_tcscpy(szNewPath, m_strCurPath);
				TCHAR *pInd = szNewPath + _tcslen(szNewPath) - 1 - 1;
				while (pInd >= szNewPath)
				{
					if ((*pInd == '\\') || (*pInd == '/'))
					{
						*pInd = '\0';
						break;
					}
					pInd--;
				}
				strPath = szNewPath;
				strPath += L"\\";

				bUpLevel = TRUE;
			}else
			{
				strPath = m_strCurPath;
				strPath += strText;
				strPath += L"\\";
			}

			DWORD dwAttr = ::GetFileAttributes(strPath);
			if ((0xFFFFFFFF != dwAttr) && (dwAttr & FILE_ATTRIBUTE_DIRECTORY))
			{
				CWceUiSliderAni *pAnimate = new CWceUiSliderAni;
				if (bUpLevel)
				{
					pAnimate->SetDirection(WCEUI_D_RIGHT);
				}else
				{
					pAnimate->SetDirection(WCEUI_D_LEFT);
				}

				CRect rc(0, 67, 480, 272);
				pAnimate->SetRect(&rc);
				CWceUiWndAnimate *p = pAnimate;
				GetWindow()->Apply();
				InitList(strPath);
				GetWindow()->Play(&p, 1);
				delete pAnimate;
			}
		}
	}
	return 0;
}

void CPhotoDlg::InitList(CString strPath)
{
	UnInitList();

	int nTotalHeight = 0;
	CString strSearch = strPath;
	strSearch += L"*.*";
	m_strCurPath = strPath;

	HANDLE hFind = INVALID_HANDLE_VALUE;
	WIN32_FIND_DATA fd = {0};

	hFind = ::FindFirstFile(strSearch, &fd);
	if (INVALID_HANDLE_VALUE != hFind)
	{
		do {
			if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
			{
				if ((0 != _tcscmp(fd.cFileName, L".")) && (0 != _tcscmp(fd.cFileName, L"..")))
				{
					LPWIN32_FIND_DATA lpData = new WIN32_FIND_DATA;
					*lpData = fd;
					lpData->nFileSizeHigh = TRUE;
					m_List.AddHead(lpData);
				}
			}else
			{
				LPWIN32_FIND_DATA lpData = new WIN32_FIND_DATA;
				*lpData = fd;
				lpData->nFileSizeHigh = FALSE;
				m_List.AddTail(lpData);
			}
		} while (::FindNextFile (hFind, &fd));

		::FindClose (hFind);
		hFind = INVALID_HANDLE_VALUE;
	}
	if (0 != _tcscmp(strPath, FB_ROOT_DIR))
	{
		LPWIN32_FIND_DATA lpData = new WIN32_FIND_DATA;
		memset(lpData, 0, sizeof(WIN32_FIND_DATA));
		_tcscpy(lpData->cFileName, L"..");
		lpData->nFileSizeHigh = TRUE;
		m_List.AddHead(lpData);
	}
	
	nTotalHeight = (int)m_List.GetCount() * PHOTO_ITEM_HEIGHT;
	if (nTotalHeight < MIN_TOTAL_ITEM_HEIGHT)
	{
		nTotalHeight = MIN_TOTAL_ITEM_HEIGHT;
	}
	m_pFrame->MoveLayer(g_nframe_left, g_nframe_top, 480, nTotalHeight);
	UpdateList();
}

void CPhotoDlg::UnInitList()
{
	POSITION pos = m_List.GetHeadPosition();
	while (pos)
	{
		delete m_List.GetNext(pos);
	}
	m_List.RemoveAll();
}

void CPhotoDlg::UpdateList()
{
	CRect rcPos;
	m_pFrame->GetWindowRect(&rcPos);

	int nTop = g_nframe_top - rcPos.top;
	nTop = max(nTop, 0);

	int nInx = nTop / PHOTO_ITEM_HEIGHT;
	int nStart = nInx * PHOTO_ITEM_HEIGHT;

	POSITION pos = m_List.FindIndex(nInx);
	int i = 0;
	while ((i < PHOTO_LIST_COUNT) && (pos))
	{
		CRect rc(0, nStart, 480, nStart + 41);
		m_pPhotoItemListArr[i]->GetCommonStyle()->SetPosition(&rc, WCEUI_RELATIVE_POSITION);
		m_pPhotoItemListArr[i]->GetCommonStyle()->SetUsePosition(TRUE);
		m_pPhotoItemListArr[i]->GetStyle()->SetPosition(&rc, WCEUI_RELATIVE_POSITION);

		LPWIN32_FIND_DATA lpData = m_List.GetNext(pos);
		m_pPhotoItemListArr[i]->SetText(lpData->cFileName);

		CWceUiBgItem* pBgItem = m_pPhotoItemListArr[i]->GetCommonStyle()->GetBgItemCtrl()->GetByIndex(0);
		ASSERT(NULL != pBgItem);
		if (lpData->nFileSizeHigh)
		{
			if (WceUiGetBitsPixel() == 16)
			{
				pBgItem->SetBitmapFromRes(L"photo/folder.565a.conf");
			}else
			{
				pBgItem->SetBitmapFromRes(L"photo/folder.32a.conf");
			}

			
		}else
		{
			if (WceUiGetBitsPixel() == 16)
			{
				pBgItem->SetBitmapFromRes(L"photo/file.565a.conf");
			}else
			{
				pBgItem->SetBitmapFromRes(L"photo/file.32a.conf");
			}
			
		}

		m_pPhotoItemListArr[i]->ShowLayer(TRUE);


		nStart +=  PHOTO_ITEM_HEIGHT;
		i++;
	}

	while (i < PHOTO_LIST_COUNT)
	{
		m_pPhotoItemListArr[i]->ShowLayer(FALSE);
		i++;
	}
}